package com.luotao.test.user;


import com.luotao.devp.base.db.BaseDao;
import com.luotao.devp.base.test.TesterConcurrent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * User: luotao
 * Date: 12-5-17
 * Time: 23:29
 */
@Service
@Transactional
public class UserService extends BaseDao {

    private static final String SQL_USER_INSERT = "insert into test_users(loginname,loginpass,mobile,nickname) values(?,?,?,?)";

    private static final String SQL_ACCT1_INSERT = "insert into acct1(userid,balc)values(?,?)";

    private static final String SQL_ACCT2_INSERT = "insert into acct2(userid,balc)values(?,?)";

    public void test(String loginname,String loginpass,String mobile,String nickname) {
        int count = getJdbcTemplate().update(SQL_USER_INSERT, loginname, loginpass, mobile, nickname);
       // System.out.println("count = " + count);
    }


    public void createAcct(){
        List<Map<String,Object>> users = getJdbcTemplate().queryForList("select userid from test_users");
        for (Map<String, Object> user : users) {
            Integer id = (Integer)user.get("userid");
            System.out.println("id = " + id);
            /*if(id%2==0){
                update(SQL_ACCT2_INSERT,id,100000);
            }
            else {
                update(SQL_ACCT1_INSERT,id,100000);
            }*/
            update(SQL_ACCT1_INSERT,id,100000);
        }
    }

    public static final String SQL_FLOW1_INSERT = "insert into test_flow1 values(?,?,?,?)";

    public static final String SQL_ACCT1_UPDATE ="update acct1 set balc=balc+? where userid=?";

    public void trade(int userid,int gameid,int amount){
        String uuid = UUID.randomUUID().toString();
        update(SQL_FLOW1_INSERT,uuid,userid,amount,gameid);
        update(SQL_ACCT1_UPDATE,amount,userid);
    }

    public static final String SQL_FLOW3_INSERT = "insert into test_flow3 values(?,?,?,?)";

    public static final String SQL_ACCT3_UPDATE ="update acct3 set balc=balc+? where userid=?";

    public void trade3(int userid,int gameid,int amount){
        String uuid = UUID.randomUUID().toString();
        update(SQL_FLOW3_INSERT,uuid,userid,amount,gameid);
        update(SQL_ACCT3_UPDATE,amount,userid);
    }

    public static final String SQL_FLOW4_INSERT = "insert into test_flow4 values(?,?,?,?)";

    public static final String SQL_ACCT4_UPDATE ="update acct4 set balc=balc+? where userid=?";

    public void trade4(int userid,int gameid,int amount){
        String uuid = UUID.randomUUID().toString();
        update(SQL_FLOW4_INSERT,uuid,userid,amount,gameid);
        update(SQL_ACCT4_UPDATE,amount,userid);
    }

    public static void main(String[] args) throws Exception {
        ApplicationContext app = new ClassPathXmlApplicationContext("spring/app-config.xml");
        final UserService userService = (UserService) app.getBean("userService");
        long cost = TesterConcurrent.runLoopInner(300, new TesterConcurrent.ITestable() {
            public void execute(long index, long threadid, int loop_index) throws Exception {

                //   String userid = StringUtils.fillZero(String.valueOf(loop_index + 1), 5);

                //      System.out.println("userid = " + userid);
                int user_index = (int) (index + 1);
                userService.trade(user_index, 1, 10);
                /*                  if(user_index%2!=0){
                    userService.trade3(user_index,1,10);
                }else{
                    userService.trade4(user_index,1,10);
                }*/
//                        userService.createAcct();
                //  userService.test();
            }
        }, 10);
//        Thread.currentThread().sleep(1000*60*10);
        System.out.println("cost = " + cost);
    }
}
